﻿package mortal.game.view.guide
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import com.gengine.global.*;
    import com.mui.controls.*;
    import extend.language.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.configBase.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.map3D.AstarAnyDirection.data.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.view.common.*;
    import mortal.game.view.mainUI.zazen.*;
    import mortal.game.view.task.data.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GuideAutoFightWin extends RightDownShrinkView
    {
        private var _txtGot:GTextFiled;
        private var _txtTime:GTextFiled;
        private var _box:GCheckBox;
        private var _btnAutoFight:GButton;
        private var _btnQuit:GButton;
        private var _passTo:SPassTo;
        private var _timer:SecTimer;
        private var _startTime:int;
        private var _oExp:Number;
        private var _oLv:int = -1;
        private var _myPt:SPassTo;
        private var _myRange:int = 600;
        private var _useFlybootLabel:int = -1;
        private var _crossServer:Boolean = false;
        private var _isWaitForFlyBoot:Boolean = false;
        private static var _instance:GuideAutoFightWin;

        public function GuideAutoFightWin(param1:ILayer = null)
        {
            super(ImagesConst.GuajiNormalDetail, ImagesConst.GuajiGet);
            this.setSize(320, 200);
            return;
        }// end function

        private function getCurAddExp() : int
        {
            var _loc_5:TExperience = null;
            var _loc_1:* = Cache.instance.role;
            var _loc_2:* = _loc_1.roleInfo.level;
            var _loc_3:* = _loc_1.roleInfo.experience - this._oExp;
            var _loc_4:* = this._oLv;
            while (_loc_4 < _loc_2)
            {
                
                _loc_5 = ConfigCenter.getConfigs(ConfigConst.expLevel, "level", _loc_4, true) as TExperience;
                _loc_3 = _loc_3 + _loc_5.upgradeNeedExperience;
                _loc_4++;
            }
            return _loc_3;
        }// end function

        public function startWork(param1:SPassTo = null, param2:int = 600, param3:int = -1, param4:Boolean = false) : void
        {
            this._myPt = param1;
            this._myRange = param2;
            this._useFlybootLabel = param3;
            this._crossServer = param4;
            this.autoFightHandler();
            this.recordMapInfo();
            this.startTimer();
            this._startTime = getTimer();
            return;
        }// end function

        public function stopWork() : void
        {
            return;
        }// end function

        private function recordMapInfo() : void
        {
            this._oLv = Cache.instance.role.roleInfo.level;
            this._oExp = Cache.instance.role.roleInfo.experience;
            this._passTo = new SPassTo();
            this._passTo.toPoint = new SPoint();
            this._passTo.mapId = Game.mapInfo.mapId;
            this._passTo.toPoint.x = RolePlayer.instance.x2d;
            this._passTo.toPoint.y = RolePlayer.instance.y2d;
            this._box.selected = PulseSharedObject.isTodayNotTips(TodayNoTipsConst.TodayNotAutoFight);
            return;
        }// end function

        private function startTimer() : void
        {
            if (this._timer == null)
            {
                this._timer = new SecTimer(1);
                this._timer.addListener(TimerType.ENTERFRAME, this.onEverySecond);
            }
            if (!this._timer.running)
            {
                this._timer.start();
            }
            return;
        }// end function

        private function onEverySecond(param1 = null) : void
        {
            this._txtGot.text = this.getCurAddExp().toString();
            var _loc_2:* = (FrameManager.flashFrame.getTime() - this._startTime) / 1000;
            var _loc_3:* = _loc_2 / 3600;
            var _loc_4:* = (_loc_2 - _loc_3 * 3600) / 60;
            this._txtTime.text = Language.getStringByParam(21018, _loc_3, _loc_4);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            var _loc_1:* = GlobalStyle.textFormatBai;
            _loc_1.size = 13;
            this._txtGot = UIFactory.gTextField("", 209, 114, 150, 22, _bodySprite, _loc_1);
            this._txtTime = UIFactory.gTextField("", 82, 114, 150, 22, _bodySprite, _loc_1.clone());
            this._box = UIFactory.checkBox(Language.getString(21019), 155, 63, 100, 28, _bodySprite);
            this._box.selected = PulseSharedObject.isTodayNotTips(TodayNoTipsConst.TodayNotAutoFight);
            this._btnAutoFight = UIFactory.gButton(Language.getString(21020), 61, 142, 65, 22, _bodySprite);
            this._btnQuit = UIFactory.gButton(Language.getString(21021), 156, 142, 65, 22, _bodySprite);
            this._box.configEventListener(Event.CHANGE, this.selectedHandler);
            this._btnAutoFight.configEventListener(MouseEvent.CLICK, this.autoFightHandler);
            this._btnQuit.configEventListener(MouseEvent.CLICK, this.quitHandler);
            Dispatcher.addEventListener(EventName.StageResize, this.stageResizeHandler);
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.GuajiTongjiLine, 0, 88, _bodySprite));
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.GuajiaNormalDes, 18, 48, _bodySprite));
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.GuajiTime, 18, 117, _bodySprite));
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.GuajiExp, 146, 117, _bodySprite));
            return;
        }// end function

        private function stageResizeHandler(param1) : void
        {
            this.y = Global.stage.stageHeight / 2;
            stageResize();
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            this.stageResizeHandler(null);
            if (ZazenIncomeView.hasInstance() && !ZazenIncomeView.instace.isHide)
            {
                ZazenIncomeView.instace.hide();
            }
            return;
        }// end function

        private function selectedHandler(param1 = null) : void
        {
            PulseSharedObject.save(TodayNoTipsConst.TodayNotAutoFight, this._box.selected);
            return;
        }// end function

        private function autoFightHandler(param1 = null) : void
        {
            var pt:SPassTo;
            var addFlyTo:Function;
            var moveToed:Function;
            var evt:* = param1;
            var addMoveTo:* = function () : void
            {
                AIManager.cancelAll();
                _isWaitForFlyBoot = true;
                if (AIManager.addMoveToOtherMap(Game.mapInfo.mapId, pt.mapId, new Point(pt.toPoint.x, pt.toPoint.y), 100, moveToed))
                {
                    if (!AIManager.isWorking)
                    {
                        AIManager.start();
                    }
                }
                return;
            }// end function
            ;
            addFlyTo = function () : void
            {
                AIManager.cancelAll();
                _isWaitForFlyBoot = true;
                AIManager.addFlyBoot(pt);
                AIManager.addDelayAI(1000);
                AIManager.addCallback(unlock);
                AIManager.addAutoFight(600);
                AIManager.start();
                return;
            }// end function
            ;
            moveToed = function () : void
            {
                AIManager.addCallback(unlock);
                AIManager.addAutoFight(_myRange);
                if (!AIManager.isWorking)
                {
                    AIManager.start();
                }
                return;
            }// end function
            ;
            if (evt != null)
            {
                this.hide();
            }
            if (this._myPt != null)
            {
                pt = this._myPt;
            }
            else
            {
                pt = GameController.taskImpl.getAutoFightPoint(-1, this._crossServer);
            }
            if (pt == null)
            {
                return;
            }
            if (pt.mapId == Game.mapInfo.mapId && GeomUtil.calcDistance(pt.toPoint.x, pt.toPoint.y, RolePlayer.instance.x2d, RolePlayer.instance.y2d) <= 400)
            {
                AIManager.onAIControl(AIType.AutoFight, this._myRange);
                return;
            }
            if (this._useFlybootLabel != -1)
            {
                if (this._useFlybootLabel == 0)
                {
                    this.addMoveTo();
                }
                else
                {
                    GameController.guide.checkAndBuyFlyBoot(addFlyTo);
                }
                return;
            }
            if (!TaskUtil.canFlyByNoBuyFlyBoot())
            {
                this.addMoveTo();
            }
            else
            {
                this.addFlyTo();
            }
            return;
        }// end function

        private function unlock() : void
        {
            this._isWaitForFlyBoot = false;
            return;
        }// end function

        public function get isWaitForFlyBoot() : Boolean
        {
            return this._isWaitForFlyBoot;
        }// end function

        private function quitHandler(param1 = null) : void
        {
            this.hide();
            if (this._passTo == null)
            {
                return;
            }
            if (TaskUtil.canFlyByNoBuyFlyBoot())
            {
                AIManager.onAIControl(AIType.FlyBoot, this._passTo);
            }
            else
            {
                AIManager.cancelAll();
                AIManager.addMoveToOtherMap(Game.mapInfo.mapId, this._passTo.mapId, new Point(this._passTo.toPoint.x, this._passTo.toPoint.y));
                AIManager.start();
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._isWaitForFlyBoot = false;
            this._txtGot.dispose(param1);
            this._txtGot = null;
            this._txtTime.dispose(param1);
            this._txtTime = null;
            this._box.dispose(param1);
            this._box = null;
            this._btnAutoFight.dispose(param1);
            this._btnAutoFight = null;
            this._btnQuit.dispose(param1);
            this._btnQuit = null;
            if (this._timer != null)
            {
                this._timer.stop();
                this._timer.dispose(false);
                this._timer = null;
            }
            Dispatcher.removeEventListener(EventName.StageResize, this.stageResizeHandler);
            return;
        }// end function

        public static function get hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

        public static function isWorking() : Boolean
        {
            if (_instance == null)
            {
                return false;
            }
            if (_instance.isHide)
            {
                return false;
            }
            return true;
        }// end function

        public static function get instance() : GuideAutoFightWin
        {
            if (_instance == null)
            {
                _instance = new GuideAutoFightWin;
            }
            return _instance;
        }// end function

    }
}
